package com.binarysoft.service.common.multiconnpool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import common.framework.dsb.DynamicService;

public interface MultipleConnectionPoolService extends DynamicService {
	/**
	 * Get data source from pool
	 * 
	 * @return Database ComboPooledDataSource
	 * @throws SQLException
	 */
	ComboPooledDataSource getDataSource(String poolname) throws Exception;
	
	/**
	 * Get connection from pool
	 * 
	 * @return Database connection
	 * @throws SQLException
	 */
	Connection getConnection(String poolname) throws Exception;

	/**
	 * add new pool from config file.
	 * 
	 * @param configFileName
	 * @throws Exception
	 */
	void loadPoolConfig(String configFileName) throws Exception;

	/**
	 * check if the specified poolname exists.
	 * 
	 * @param poolname
	 * @return
	 */
	boolean exists(String poolname);

	/**
	 * List all pool names
	 * 
	 * @return all pool names
	 */
	Collection<String> listPools();

	/**
	 * Get number of busy connections from the specified pool
	 * 
	 * @param poolname
	 *            the name of specified pool
	 * @return number of busy connections
	 * @throws Exception
	 */
	int getNumBusyConnections(String poolname) throws Exception;

	/**
	 * Get number of connections from the specified pool
	 * 
	 * @param poolname
	 *            the name of specified pool
	 * @return number of busy connections
	 * @throws Exception
	 */
	int getNumConnections(String poolname) throws Exception;

	/**
	 * Get number of idle connections from the specified pool
	 * 
	 * @param poolname
	 *            the name of specified pool
	 * @return number of busy connections
	 * @throws Exception
	 */
	int getNumIdleConnections(String poolname) throws Exception;

	/**
	 * Get initial pool size of the specified pool.
	 * 
	 * @param poolname
	 *            the name of specified pool
	 * @return
	 * @throws Exception
	 */
	int getInitialPoolSize(String poolname) throws Exception;

	/**
	 * Get stack trace of unclosed connections
	 * 
	 * @param poolname
	 *            specified pool name
	 * @return
	 */
	Map<Connection, String> getStackTrace();
}
